package com.niuwa.p2p.action;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.interceptor.ParameterAware;

import com.niuwa.p2p.entity.PageQuery;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName: BaseAction
 * @Description: TODO
 * @author: zhangbo
 * @date:2015年1月9日 上午9:20:54
 */
public class BaseAction extends ActionSupport implements ParameterAware {

	private static final long serialVersionUID = -3294079453489164016L;

	public static final String ADD = "add";

	public static final String SAVE = "save";

	public static final String DELETE = "delete";

	public static final String EDIT = "edit";

	public static final String UPDATE = "update";

	public static final String INDEX = "index";

	public static final String LIST = "list";

	public static final String TREE = "tree";

	public static final String DETAIL = "detail";

	public static final String PRINT = "print";

	public static final String ADD_OR_EDIT = "addOrEdit";

	public static final String INFO = "info";

	public static final String PASSWORD = "password";

	public static final String IMPORT = "import";

	public static final String EXPORT = "export";

	public static final String DOWNLOAD = "download";

	public static final String AUDIT = "audit";

	public static final String REVIEW = "review";

	public static final String LOCK = "lock";

	public static final String ACTIVE = "active";

	public static final String CERTIFI = "certifi";

	protected Map<String, String[]> parameters;

	private PageQuery pageQuery;
	
	private boolean is_ajax;

	public BaseAction() {
	}

	@Action("index")
	public String index() {
		return INDEX;
	}

	/**
	 * crud之C,跳转到增加页面
	 * 
	 * @return
	 */
	@Action("add")
	public String add() {
		return ADD;
	}

	/**
	 * crud之C,新增页面提交之后跳转的方法,用于表单数据的持久化
	 * 
	 * @return
	 */
	@Action("save")
	public String save() {
		return SAVE;
	}

	/**
	 * cru之U,跳转到更新页面
	 * 
	 * @return
	 */
	@Action("edit")
	public String edit() {
		return EDIT;
	}

	/**
	 * crud之U,执行更新操作,将更新数据持久化
	 * 
	 * @return
	 */
	@Action("update")
	public String update() {
		return UPDATE;
	}

	/**
	 * crud之R,以列表方式展示多条记录
	 * 
	 * @return
	 */
	@Action("list")
	public String list() {
		return LIST;
	}

	/**
	 * 跳转到单条记录的详细信息页面
	 * 
	 * @return
	 */
	@Action("detail")
	public String detail() {
		return DETAIL;
	}

	/**
	 * 树形展示页面
	 * 
	 * @return
	 */
	@Action("tree")
	public String tree() {
		return TREE;
	}

	/**
	 * crud之d,删除记录
	 * 
	 * @return
	 */
	@Action("delete")
	public String delete() {
		return DELETE;
	}

	/**
	 * 导入文件
	 * 
	 * @return
	 */
	@Action("import")
	public String importFile() {
		return IMPORT;
	}

	/**
	 * 导出文件
	 * 
	 * @return
	 */
	@Action("export")
	public String exportFile() {
		return EXPORT;
	}

	/**
	 * 打印
	 * 
	 * @return
	 */
	@Action("print")
	public String print() {
		return PRINT;
	}

	/**
	 * 下载文件
	 * 
	 * @return
	 */
	@Action("download")
	public String download() {
		return DOWNLOAD;
	}

	/**
	 * 锁定
	 * 
	 * @return
	 */
	@Action("lock")
	public String lock() {
		return LOCK;
	}

	/**
	 * 激活
	 * 
	 * @return
	 */
	@Action("active")
	public String active() {
		return ACTIVE;
	}

	/**
	 * 审核
	 * 
	 * @return
	 */
	@Action("audit")
	public String audit() {
		return AUDIT;
	}

	/**
	 * 复核
	 * 
	 * @return
	 */
	@Action("review")
	public String review() {
		return "REVIEW";
	}

	@Override
	public void setParameters(Map<String, String[]> parameters) {
		this.parameters = parameters;

	}

	public Map<String, Object> getParamMap() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		Iterator<Entry<String, Object>> ltr = ActionContext.getContext()
				.getActionInvocation().getInvocationContext().getParameters()
				.entrySet().iterator();

		while (ltr.hasNext()) {
			Entry<String, Object> e = ltr.next();
			if (e.getValue() instanceof String) {
				paramMap.put(e.getKey(), e.getValue());
			} else if (e.getValue() instanceof String[]) {
				String[] value = (String[]) e.getValue();
				if (value.length == 1) {
					paramMap.put(e.getKey(), value[0]);
				} else {
					paramMap.put(e.getKey(), Arrays.asList(e.getValue()));
				}
			}
		}
		if (getPageQuery() == null) {
			//PageQuery pageQuery=new PageQuery(Long.valueOf(paramMap.get("page").toString()),Long.valueOf(paramMap.get("rows").toString()));
			PageQuery pageQuery = null;
			Object page =  paramMap.get("page");
			if(page == null){
				pageQuery = new PageQuery();
			}else{
				pageQuery=new PageQuery(Long.valueOf(paramMap.get("page").toString()),Long.valueOf(paramMap.get("rows").toString()));
			}
			//如果分页有排序将属性转换成列名
			/*String sort = (String)paramMap.get("sort");
			if(sort != null && !"".equals(sort)){
				sort = proToCol(sort);
				paramMap.put("sort", sort);
			}*/
			paramMap.put("pageQuery", pageQuery);
		} 
//		else {
//			paramMap.put("pageQuery", new PageQuery());
//		}
		return paramMap;
	}

	public String getParam(String name) {
		String[] params = getParams(name);
		if (params != null && params.length == 1) {
			return params[0];
		}
		return null;
	}

	public String[] getParams(String name) {
		return this.parameters.get(name);
	}

	public void setPageQuery(PageQuery pageQuery) {
		this.pageQuery = pageQuery;
	}

	public PageQuery getPageQuery() {
		return pageQuery;
	}
		
	public boolean isIs_ajax() {
		return is_ajax;
	}

	public void setIs_ajax(boolean is_ajax) {
		this.is_ajax = is_ajax;
	}

	/**
	 * 将属性转换成数据库对应字段
	 * @param pro
	 * @return
	 */
	private String proToCol(String pro){
		StringBuilder sb = new StringBuilder();
		for (int charInd = 0; charInd < pro.length(); ++charInd) {
			final char valueChar = pro.charAt(charInd);
			if(valueChar >= 'A' && valueChar <= 'Z'){
				sb.append("_");
			}
			sb.append(valueChar);
		}
		return sb.toString().toUpperCase();
	}
}
